package two_pointer;

import linkedlist.ListNode;

/**
 * Created with IntelliJ IDEA.
 * Description: 142. 环形链表 II
 * User: Li_yizYa
 * Date: 2025/5/15
 * Time: 22:33
 */
public class Solution8 {
    public ListNode detectCycle(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        if (fast == null || fast.next == null) {
            return null;
        }
        fast = head;
        while (fast != null && slow != null) {
            if (fast == slow) {
                return fast;
            }
            fast = fast.next;
            slow = slow.next;
        }
        return null;
    }
}
